package com.wsc.wisdomchina.houselease.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.adapter.FloorSearchAdapter;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.houselease.adapter.HouseLeaseSearchAdapter;
import com.wsc.wisdomchina.houselease.bean.HouseLeaseSearchBean;
import com.wsc.wisdomchina.javaBean.FloorInfoSearchBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.presenter.ContributorPresenter;

import java.util.List;

/**
 * 楼宇搜索页面
 * Created by ${wangtao} on 2017/3/14.
 */

public class HouseLeaseSearchActivity extends BaseActivity implements View.OnClickListener,ContributorView{

    private EditText fragment_lyss_title;//搜索文字
    private TextView fragment_lyss_back;//取消
    private ListView fragment_lyss_listView;//列表
    private ContributorPresenter contributorPresenter;
    private Context context;
    private final String BUILDING_SEARCH_FLAG="BUILDING_SEARCH_FLAG";
    private List<HouseLeaseSearchBean> listdata;
    private String cidNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.house_lease_search_layout);
        initView();
        context=this;
        cidNumber=getIntent().getStringExtra("cid");
        contributorPresenter=new ContributorPresenter(this,context);
    }

    private void initView()
    {
        fragment_lyss_title=(EditText) findViewById(R.id.fragment_lyss_title);//搜索文字
        fragment_lyss_back=(TextView) findViewById(R.id.fragment_lyss_back);//取消
        fragment_lyss_listView=(ListView) findViewById(R.id.fragment_lyss_listView);;//列表
        fragment_lyss_back.setOnClickListener(this);
        fragment_lyss_title.setHint("房屋租赁搜索");
        fragment_lyss_title.setHintTextColor(getResources().getColor(R.color.white));
        Tools.showSoftInputFromWindow(this,fragment_lyss_title);
        //监听输入框
        fragment_lyss_title.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int before,
                                          int count) {
                //这是文本框改变之前会执行的动作
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                //这个应该是在改变的时候会做的动作吧
            }

            @Override
            public void afterTextChanged(Editable s) {
                //这是文本框改变之后 会执行的动作
                String textView = fragment_lyss_title.getText().toString().trim();
                if (textView != null) {
                    contributorPresenter.getHouseSearch(cidNumber,textView,BUILDING_SEARCH_FLAG);
                }
            }
        });

        fragment_lyss_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HouseLeaseSearchBean floorInfoSearchBean=listdata.get(position);
                Intent intent=new Intent(context,HouseLeaseDetailActivity.class);
                intent.putExtra("bid",floorInfoSearchBean.getId());
                intent.putExtra("classname",HouseLeaseSearchActivity.class.getSimpleName());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(BUILDING_SEARCH_FLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    listdata = JsonTools.jsonStringToList(jsonMessage, HouseLeaseSearchBean.class);
                    if (listdata != null && listdata.size()>0) {
                        fragment_lyss_listView.setVisibility(View.VISIBLE);
                        HouseLeaseSearchAdapter adapter = new HouseLeaseSearchAdapter(context, listdata);
                        fragment_lyss_listView.setAdapter(adapter);
                    }else
                    {
                        fragment_lyss_listView.setVisibility(View.GONE);
                    }

                }else
                {
                    fragment_lyss_listView.setVisibility(View.GONE);
                }
            }
        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.fragment_lyss_back:
                finish();
                break;
        }
    }

}
